#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QMessageBox>
#include <QThread>

#include <QPlainTextEdit>
#include <QComboBox>
#include <QPushButton>
#include <QLineEdit>
#include <QScrollArea>
#include <QLabel>
#include <QCheckBox>

#include <QGridLayout>
#include <QVBoxLayout>

#include <QScrollArea>

#include <vector>
#include <math.h>
#include <QButtonGroup>
#include <QDebug>
#include <QKeyEvent>
#include <QTimer>

#include <stdlib.h>


class Widget;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();


private slots:
    //连接相关
    void scanport_slot();
    void openport_slot();

    //发送接收
    void MainSerialRecvMsgEvent();
    void MainSerialSendMsgEvent();
    qint64 MainSerialSendMsgEvent(char *qbarr, qint64 len);
    void ScrollSerialSend_slot();
    void RecFinish_slot();

    //按键服务
    void AddScrCom();
    void AddScrComString(QString i);
    void ContinSendButtonFun();
    void Precise_ContinSendFun();
    void StopContinSendFun();

    //延时服务函数，延时发送
    void DelayTimer_Fun();
    //超时服务函数
    void OverTimer_Fun();

private:
    Ui::Widget *ui;

    QSerialPort *MainSerial;
    QByteArray MainSerialRecvData;

    QTextCodec *codec;
    QTimer *DelayTimer;
    QTimer *OverTimer;

    int MainSerialPortRecvFrameNumber;
    int MainSerialPortRecvErrorFrameNumber;
    int MainSerialPortOneFrameSize;
    int MainSerialBaudRate;

//SERIAL
    QPushButton *OpenButton;

    QPlainTextEdit *RecvText;
    QLineEdit *SendText;
    QComboBox *ComboBox;

    QTimer *SerialDelayTimer;


//坐标参数显示
    std::vector <QLabel*> ArmVal_label;
    std::vector <QLabel*> ArmValLab_label;

//Scroll外体控件
    QScrollArea *scrollarea;
    QWidget *ScrollWidget;
    QGridLayout *ScrollLayout;

//Scroll内部控件
    std::vector <QPushButton*> Scroll_button;   //按键
    std::vector <QLineEdit*> Scroll_lineedit;   //指令框
    std::vector <QLineEdit*> Scroll_delay_lineedit; //延时框
    std::vector <QCheckBox*> Scroll_checkbox;   //使能连续发送选项框

    std::vector <QLabel*> comment;

//Scroll下方控件
    //text and send label/checkbox
    QCheckBox *ContinSend_CheckBox;
    QCheckBox *ContinSend_Precise_CheckBox;

    QLabel *ChatStatu_Label;

    //text and send button
    QPushButton *AddScrComButton;

    QPushButton *ContinSendButton;
    QPushButton *ContinSend_Precise_Button;

    QPushButton *StopContinSendButton;

//cv搜索控件
    //open carmera
    QPushButton *Open_Close_Carmera;

    //search connect
    QPushButton *SearchCupButton;
    QLabel *SearchCupLabel;

    //AUTO run
    QLabel *AutoRunLabel;
    QCheckBox *AutoRunCheckBox;
    QPushButton *AutoRunButton;

    //search apart
    QPushButton *SearchCup_Dect_Button;
    QPushButton *SearchCup_Dis_Button;


    //
    QGridLayout *layout_grid,*layout_grid2,*layout_grid3;
    QVBoxLayout *layout_main;

private:

#define VerifyStatu 12
#define ExecuteStatu 24
#define FreeStatu 48

    unsigned int ChatStatu;
    unsigned int ContinIndex;
    unsigned int Precise_ContinIndex;

    unsigned int AutoRunIndex;

    bool RepeatFlag;

//对serial接收的参数进行解析
    void Handle_RecMsg(QString str);
    int Handle_RecMsgStdToDou(QString str, QString dis_str, double &result);


//
    void AutoRun_Fun();

//自动发送
    void AutoContinSend();
    int ContinSend();

//带自校准、准确的自动发送
    void AutoContinSend_Precise();
    int ContinSend_Precise();
};



int memccpy_rid(char *dest, char *src, unsigned char c, unsigned int count);
char* strstr_rid(const char * str1, const char * str2 );
int AnalyCommand(char *originate, char *command,double* command_num);    //定长指令


#endif // WIDGET_H
